package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.FoodListAdapter;
import com.xgkj.diyiketang.data.FoodInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.FoodListTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/17.
 */
public class BendiFoodListActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener{
    private Context mContext;
    private PullToRefreshListView mListView;
    private FoodListAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private String areaName;
    private int mBDfoodTaskId = -1;
    private TextView back ;
    private String areaId ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bendifoodlist);
        mContext = this;
        initView();
    }

    private void initView(){
        Intent intent = getIntent();
        areaName = intent.getStringExtra("AreaName");
//        PalLog.e("adapter位置", areaName);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (PullToRefreshListView)findViewById(R.id.bdfoodlist);
        mListView.init(mContext,true);
        mAdapter = new FoodListAdapter(mContext);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mListView.getHeaderViewsCount()>0){
                    i--;
                }
                if (i < 0 || i>=mAdapter.getCount()) return;
                FoodInfo fdinfo = (FoodInfo) mAdapter.getItem(i);
                String fdid = fdinfo.getfoodId();
                Intent intent = new Intent(mContext,SellerListActivity.class);
                intent.putExtra("FOODID",fdid);
                intent.putExtra("AREAID",areaId);
                startActivity(intent);
            }
        });
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
    }

    private void getBDFoodList() {
        if(mBDfoodTaskId != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        FoodListTask task = new FoodListTask(size, currentPage,areaName );
        task.addListener(bdmoreFoodListListener);
        mBDfoodTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener bdmoreFoodListListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == mBDfoodTaskId){
                mBDfoodTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                }else{
                    mListView.onLoadDataComplete(true);
                }
                List<FoodInfo> mfoodlist = new ArrayList<FoodInfo>();
                if(data != null){
                    //mfoodlist = (List<FoodInfo>) data;
                    int pageCount = 0;
                    int currentPage = 0;
                    int nextPage = 0;

                    JSONObject jsonString = (JSONObject) data;
                    currentPage = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
                     pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
                     nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
                    areaId = jsonString.getString("areaId");
                    mfoodlist = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), FoodInfo.class);
                }

                if(mfoodlist != null && mfoodlist.size()>0){
                    mAdapter.updateData(mfoodlist);
                }

                currentPage = result;


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == mBDfoodTaskId){
                mBDfoodTaskId = -1;
                if(currentPage == 1){
                    mListView.onRefreshComplete();
                }else{
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };



    public void onHiddenChanged(boolean hidden) {
        onHiddenChanged(hidden);
        if(!hidden){
            //mListView.invalidate();
            if(mAdapter != null && mAdapter.getCount() == 0){
                mListView.doRefresh();
            }

        }
    }
    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getBDFoodList();
    }

    @Override
    public void onRefresh() {
        currentPage=1;
        getBDFoodList();
    }

//    @Override
//    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//        Intent intent = new Intent(mContext,LoginActivity.class);
//        startActivity(intent);
//    }
}
